/**
 * Created by scenic on 2017/7/15.
 */

var start, scene, spaceRoot;
;

var BusinessTestUtils = {

    getStart: function () {
        if (start) {
            start.dispose();
        }

        document.getElementById("main_test").style.display = "block";
        var container = document.getElementById("container");
        var canvas = document.getElementById("rd_three");
        start = new GSThree.StartNB({canvas: canvas, container: container});
        return start;
    },

    getScene: function () {
        if (scene) {
            return scene;
        }
        scene = this.getStart().newScene("main");
        scene.sceneColor = "#000000"
        new GSThree.LightConfig(scene).addAmbientLight().addDirectionLight();

        function refresh() {
            requestAnimationFrame(refresh);
            start.clearCanvas();
            start.renderMultipleScene();
        }

        refresh();

        return scene;
    },
    addToScene: function (space) {


        space = space == undefined ? spaceRoot : space;

        if (scene == undefined) {
            this.getScene();
        }
        //增加mesh
        space.traverse(function () {
            if (this.spaceMesh.isAddToScene)
                scene.add(this.spaceMesh)
            this.faces.forEach(function (face) {
                if (face.getAssociateMesh().isAddToScene)
                    scene.add(face.getAssociateMesh())
            })

            // this.updateSpaceMeshMaterial();

        })

        GSThree.Utils.showSceneResource(scene, 'resource');
    },
    hideAllFrontPlank: function () {
        spaceRoot.traverse(function () {
            this.frontFace.getAssociateMesh().visible = false;
        })
    },

    showSpaceWithLine: function () {
        spaceRoot.traverse(function () {
            GSThree.Utils.transparentMesh(this.getAssociateMesh(), true);
        })
    }
}


describe("BoxSpace", function () {

    it("toTreeFolder", function () {
        var space = new GSResource.BoxSpace(1400,900,500);
        space.setSegment("1:1:1",new THREE.Vector3(1,0,0));
        space.updateSegment();

        space.toTreeFolder().traverse(function (f) {
            console.log(f.id);
        })
        console.log("================")
        space.toTreeFolder().traverse(function (f) {
            console.log(f.id);
        })



        expect(1).toBe(1);
    })
    it("updatePlankOffset",function () {

    })

    it("updateSegment", function () {
        /*
         测试指令
         1. 所有的面没有厚度
         spaceRoot.faces.forEach(function (f) {f.getAssociateMesh().isAddToScene = false});updateSpaceSegment();

         2. 所有的面没有厚度
         spaceRoot.faces.forEach(function (f) {f.getAssociateMesh().isAddToScene = true});updateSpaceSegment();

         3.
         spaceRoot.leftFace.getAssociateMesh().isAddToScene = true;updateSpaceSegment();
         spaceRoot.rightFace.getAssociateMesh().isAddToScene = true;updateSpaceSegment();
         spaceRoot.topFace.getAssociateMesh().isAddToScene = true;updateSpaceSegment();
         spaceRoot.bottomFace.getAssociateMesh().isAddToScene = true;updateSpaceSegment();
         spaceRoot.frontFace.getAssociateMesh().isAddToScene = true;updateSpaceSegment();
         spaceRoot.backFace.getAssociateMesh().isAddToScene = true;updateSpaceSegment();


         spaceRoot.setSegment("1:2:1", new THREE.Vector3(-1, 0, 0).normalize());spaceRoot.updateSegment();

         */


        var width = 1600, height = 900, depth = 500;

        var space = new GSResource.BoxSpace(width, height, depth);

        space.setSegment("1:2:1", new THREE.Vector3(1, 0, 0));
        space.updateSegment();

        var children = space.children_space;
        //检测孩子空间的位置是否正确
        expect(children[0].position.x).toBe(-600);
        expect(children[1].position.x).toBe(0);
        expect(children[2].position.x).toBe(600);

        //检测孩子空间的尺寸是否正确
        expect(children[0].dimension.x).toBe(400);
        expect(children[1].dimension.x).toBe(800);
        expect(children[2].dimension.x).toBe(400);

        //检测父空间的面增加板子之后，孩子空间的位置是否正确
        //===================start=========================

        //父空间不加板子
        space.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = true
        });
        space.updateSegment();
        expect(children[0].position.x).toBe(-600 + 7 / 8 * space.leftFace.getAssociateThickness() - 1 / 8 * space.rightFace.getAssociateThickness());
        expect(children[1].position.x).toBe(0);
        expect(children[2].position.x).toBe(600 - 7 / 8 * space.rightFace.getAssociateThickness() + 1 / 8 * space.leftFace.getAssociateThickness());

        //所有的面都增加了板子
        space.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = true
        })
        space.updateSegment();
        expect(children[0].position.x).toBe(3 / 4 * space.leftFace.getAssociateThickness() - 3 / 8 * 1600);
        expect(children[0].position.y).toBe(0);

        //只有左侧和上册有板子
        space.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = false
        });
        space.leftFace.getAssociateMesh().isAddToScene = true;
        space.topFace.getAssociateMesh().isAddToScene = true;
        space.updateSegment();
        expect(children[0].position.x).toBe(7 / 8 * space.leftFace.getAssociateThickness() - 3 / 8 * 1600);
        expect(children[0].position.y).toBe(-0.5 * space.topFace.getAssociateThickness());

        //只增加右侧和底测的板子
        space.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = false
        })
        space.rightFace.getAssociateThickness().isAddToScene = true;
        space.updateSegment();
        expect(children[0].position.x).toBe(3 / 4 * space.rightFace.getAssociateThickness() - 3 / 8 * 1600);

        //只增加底部
        space.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = false
        })
        space.bottomFace.getAssociateThickness().isAddToScene = true;
        expect(children[0].position.y).toBe(0.5 * space.bottomFace.getAssociateThickness());
        //只增加顶部
        space.faces.forEach(function (f) {
            f.getAssociateMesh().isAddToScene = false
        })
        space.topFace.getAssociateThickness().isAddToScene = true;
        expect(children[0].position.y).toBe(-0.5 * space.topFace.getAssociateThickness());

        //===================end=========================

    })

})